﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using IfSoft.OA.Entities;

namespace IfSoft.OA.DataAccess
{
    public class BankBillRepository : RepositoryBase, IBankBillRepository
    {
        #region Basic CRUD operations

        public void Create(BankBill bo)
        {
            using (var context = GetContext())
            {
                context.BankBills.Add(bo);
                context.SaveChanges();
            }
        }
       
        public void Update(BankBill bo)
        {
            using (var context = GetContext())
            {
                //var update = context.BankBills.FirstOrDefault(e => e.Id == bo.Id);
                context.Entry(bo).State = EntityState.Modified;
                context.SaveChanges();
            }
        }

        public void Delete(int id)
        {
            using (var context = GetContext())
            {
                var delete = context.BankBills.Find(id);
                context.BankBills.Remove(delete);
                context.SaveChanges();
            }
        }

        public BankBill SearchById(int id)
        {
            using (var context = GetContext())
            {
                return context.BankBills.Include("BankBillDetails").FirstOrDefault(b=>b.Id == id);
            }
        }

        public List<BankBill> Search(BankBillCriteria criteria)
        {
            using (var context = GetContext())
            {
                return context.BankBills.ToList();
            }
        }

        public PagedList<BankBill> SearchPagedList(BankBillCriteria criteria)
        {
            using (var context = GetContext())
            {               
                IQueryable<BankBill> query = context.BankBills.Include("BankBillDetails");
                if (criteria.Id != null && criteria.Id != 0)
                    query = query.Where(bo => bo.Id == criteria.Id);
                if (!string.IsNullOrEmpty(criteria.PayCompany))
                    query = query.Where(bo=>bo.PayCompany.Contains(criteria.PayCompany));
                if (!string.IsNullOrEmpty(criteria.PayBank))
                    query = query.Where(bo => bo.PayBank.Contains(criteria.PayBank));
                if (!string.IsNullOrEmpty(criteria.PayType))
                    query = query.Where(bo => bo.PayType == criteria.PayType);
                if (!string.IsNullOrEmpty(criteria.PayNoteNo))
                    query = query.Where(bo => bo.PayNoteNo.Contains(criteria.PayNoteNo));
                if (!string.IsNullOrEmpty(criteria.ReceiveCompany))
                    query = query.Where(bo => bo.ReceiveCompany.Contains(criteria.ReceiveCompany));
                if (!string.IsNullOrEmpty(criteria.ReceiveBank))
                    query = query.Where(bo => bo.ReceiveBank.Contains(criteria.ReceiveBank));
                if (!string.IsNullOrEmpty(criteria.ReceiveAccountNo))
                    query = query.Where(bo => bo.ReceiveAccountNo.Contains(criteria.ReceiveAccountNo));

                query = query.SortBy("Id", true);
                return new PagedList<BankBill>(query, criteria.StartIndex, criteria.PageSize);
            }
        }

        #endregion       
    }
}
